﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class PlayerRenascence : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Player_PlayerRenascence;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<PlayerRenascenceReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}

			if (session.minecraftSessionInfo.player.Human_Life > 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：session.minecraftSessionInfo.player.Human_Life > 0）" });
				return;
			}
			session.minecraftSessionInfo.player.PrevCorpseId = session.minecraftSessionInfo.player.CurCorpseId;
			session.minecraftSessionInfo.player.CurCorpseId = req.CurCorpseId;
			session.minecraftSessionInfo.map.SourceMapId = MinecraftConfig.BaseMapId;
			session.minecraftSessionInfo.player.Human_Life = MinecraftConfig.PlayerInitHp;
			session.minecraftSessionInfo.player.Human_Hunger = 100;
			List<KeyValueEnumAttributeInt> attributeList = new List<KeyValueEnumAttributeInt>();
			attributeList.Add(new KeyValueEnumAttributeInt() { key = EnumAttribute.CurHp, value = session.minecraftSessionInfo.player.Human_Life });
			attributeList.Add(new KeyValueEnumAttributeInt() { key = EnumAttribute.CurFeed, value = session.minecraftSessionInfo.player.Human_Hunger });
			//给客户端物品信息
			var cellList = session.minecraftSessionInfo.goodsList.Where(m => m.enumGoodsBelongsToType_Convert == EnumGoodsBelongsToType.PlayerCorpse && m.guid == "").GetBackpackGoodsCellInfo();

			var resp = new PlayerRenascenceResp
			{
				attributeList = attributeList,
				CellList = cellList,
				LastDeathMapId = session.minecraftSessionInfo.player.LastDeathMapId,
				LastDeathPoint = new Point3
				{
					x = session.minecraftSessionInfo.player.LastDeathPointX,
					y = session.minecraftSessionInfo.player.LastDeathPointY,
					z = session.minecraftSessionInfo.player.LastDeathPointZ
				},
				CurCorpseId = session.minecraftSessionInfo.player.CurCorpseId,
				PrevCorpseId = session.minecraftSessionInfo.player.PrevCorpseId
			};
			session.Send(defCommand, req, resp);
		}
	}
}
